﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Security.Cryptography;

namespace DentistApplication
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class LoginWindow : Window
    {
        public LoginWindow()
        {
            DatabaseHandler.connectToDatabase();         
        }

        private void bt_login_Click(object sender, RoutedEventArgs e)
        {

           
         
           string pwd = Md5Encrypt(box_pssword.Password);
           if (DatabaseHandler.readPassword(box_login.Text) == pwd) 
           {
               MainWindow mw = new MainWindow(box_login.Text);

           
               mw.Show();
               this.Close();
           }
           else
           {
               MessageBox.Show("nie znasz hasła!");
           }
        }

        public string Md5Encrypt(string originalPassword)
        {
            //Declarations
            Byte[] originalBytes;
            Byte[] encodedBytes;
            MD5 md5;

            //Instantiate MD5CryptoServiceProvider, get bytes for original password and compute hash (encoded password)
            md5 = new MD5CryptoServiceProvider();
            originalBytes = ASCIIEncoding.Default.GetBytes(originalPassword);
            encodedBytes = md5.ComputeHash(originalBytes);

            //Convert encoded bytes back to a 'readable' string
            return BitConverter.ToString(encodedBytes);
        }

        private void box_password_TextChanged(object sender, TextChangedEventArgs e)
        {

        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {

            DentistApplication.dentAppBaseDataSet dentAppBaseDataSet = ((DentistApplication.dentAppBaseDataSet)(this.FindResource("dentAppBaseDataSet")));
            // Load data into the table Personel. You can modify this code as needed.
            DentistApplication.dentAppBaseDataSetTableAdapters.PersonelTableAdapter dentAppBaseDataSetPersonelTableAdapter = new DentistApplication.dentAppBaseDataSetTableAdapters.PersonelTableAdapter();
            dentAppBaseDataSetPersonelTableAdapter.Fill(dentAppBaseDataSet.Personel);
            System.Windows.Data.CollectionViewSource personelViewSource = ((System.Windows.Data.CollectionViewSource)(this.FindResource("personelViewSource")));
            personelViewSource.View.MoveCurrentToFirst();
        }

        private void id_roleTextBox_TextChanged(object sender, TextChangedEventArgs e)
        {

        }

        private void id_roleComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {

        }

        private void box_login_TextChanged(object sender, TextChangedEventArgs e)
        {

        }

        private void button2_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            MessageBox.Show("Wszelkie pytania proszę kierowac pod adres mailowy wisiienka@gmail.com. \r\nOdpowiedż w ciągu 24h.","Pomoc");
        }
    }
}
